/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfohelp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.campusinfohelp.entity.CampusinfoProDetail;
import com.lczyfz.campusinfo.modules.campusinfohelp.service.CampusinfoProDetailService;
import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfohelp.entity.CampusinfoProblem;
import com.lczyfz.campusinfo.modules.campusinfohelp.service.CampusinfoProblemService;

import java.util.List;

/**
 * 问题列表Controller
 * @author zms
 * @version 2018-04-16
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfohelp/campusinfoProblem")
public class CampusinfoProblemController extends BaseController {
	@Autowired
	private CampusinfoProDetailService campusinfoProDetailService;
	@Autowired
	private CampusinfoProblemService campusinfoProblemService;
	@Autowired
	private SystemService systemService;
	@ModelAttribute
	public CampusinfoProblem get(@RequestParam(required=false) String id) {
		CampusinfoProblem entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoProblemService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoProblem();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfohelp:campusinfoProblem:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoProblem campusinfoProblem, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoProblem> page = campusinfoProblemService.findPage(new Page<CampusinfoProblem>(request, response), campusinfoProblem);
        List<CampusinfoProblem> campusinfoUseres = campusinfoProblemService.findList(campusinfoProblem);
		model.addAttribute("campusinfoUseres", campusinfoUseres);
		model.addAttribute("page", page);
		return "modules/campusinfohelp/campusinfoProblemList";
	}

	@RequiresPermissions("campusinfohelp:campusinfoProblem:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoProblem campusinfoProblem, Model model) {
		User campusinfoUser = new User();
		List<User> campusinfoUseres = systemService.findUser(campusinfoUser);
		model.addAttribute("campusinfoUseres", campusinfoUseres);
		model.addAttribute("campusinfoProblem", campusinfoProblem);
		return "modules/campusinfohelp/campusinfoProblemForm";
	}

	@RequiresPermissions("campusinfohelp:campusinfoProblem:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoProblem campusinfoProblem, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoProblem)){
			return form(campusinfoProblem, model);
		}
		campusinfoProblemService.save(campusinfoProblem);
		addMessage(redirectAttributes, "保存问题列表成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfohelp/campusinfoProblem/?repage";
	}
	
	@RequiresPermissions("campusinfohelp:campusinfoProblem:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoProblem campusinfoProblem, RedirectAttributes redirectAttributes) {
		campusinfoProblemService.delete(campusinfoProblem);
		CampusinfoProDetail findcampusinfoProDetai = new CampusinfoProDetail();
		findcampusinfoProDetai.setProId(campusinfoProblem.getId());
		List<CampusinfoProDetail> campusinfoProDetails =  campusinfoProDetailService.findList(findcampusinfoProDetai);
		for(int i=0; i<campusinfoProDetails.size();i++) {
			CampusinfoProDetail campusinfoProDetail = campusinfoProDetails.get(i);
			campusinfoProDetailService.delete(campusinfoProDetail);
		}
		addMessage(redirectAttributes, "删除问题列表成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfohelp/campusinfoProblem/?repage";
	}

}